package com.demo.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKDynamicProxy implements InvocationHandler {

    /**
     * 声明被代理对象
     */
    private Person person;

    /**
     * 构造函数
     * @param person
     */
    public JDKDynamicProxy(Person person) {
        this.person = person;
    }

    /**
     * 获取代理对象
     * @return
     */
    public Object getTarget(){
        return Proxy.newProxyInstance(person.getClass().getClassLoader(),person.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("原方法前置增强");
        
        //原方法执行
        Object invoke = method.invoke(person, args);

        System.out.println("原方法后置增强");
        
        return invoke;
    }
}
